package com.cssw.fyzb.core.dao.turntable;

import com.cssw.fyzb.core.model.dodb.AcctTransfer;
import com.cssw.fyzb.core.model.dodb.TurntableLog;
import com.cssw.fyzb.core.model.dodb.TurntableWin;
import com.cssw.fyzb.core.util.TimeUtil;
import com.ydn.dbframe.plugin.activerecord.Db;
import com.ydn.dbframe.plugin.activerecord.Page;
import com.ydn.dbframe.plugin.activerecord.parse.SqlParse;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class TurntableLogDaoImpl implements TurntableLogDao {
    @Override
    public Integer getTurnNumToday(Long userId) {
        Date now = new Date();
        int dayTime = TimeUtil.getTimeYYMMDD(now);
        String sql = "select count(log_id) as num from t_turntable_log \n" +
                " where user_id = ? and day_time = ? ";

        Integer res = Db.queryInt(sql, userId, dayTime);
        return res == null ? 0 : res;
    }

    @Override
    public Long addTurnLog(Long userId, Long turntableId, String price, String turntableName, String payType, String win) {
        TurntableLog turntableLog = new TurntableLog();
        turntableLog.setUserId(userId);
        turntableLog.setTurntableId(turntableId);
        turntableLog.setPrice(price);
        turntableLog.setTurntableName(turntableName);
        turntableLog.setPayType(payType);
        turntableLog.setWin(win);
        Date now = new Date();
        int dayTime = TimeUtil.getTimeYYMMDD(now);
        turntableLog.setDayTime(dayTime);
        turntableLog.setCreateTime(now);
        turntableLog.save();
        return turntableLog.getLogId();
    }

    @Override
    public Long addTurnWinLog(Long logId, Long userId, Long turntableId, String type, String turntableName, String thumb, String status) {
        TurntableWin turntableWin = new TurntableWin();
        turntableWin.setLogId(logId);
        turntableWin.setUserId(userId);
        turntableWin.setTurntableId(turntableId);
        turntableWin.setType(type);
        turntableWin.setTurntableName(turntableName);
        turntableWin.setThumb(thumb);
        Date now = new Date();
        turntableWin.setStatus(status);
        turntableWin.setCreateTime(now);
        turntableWin.save();
        return turntableWin.getWinId();
    }

    @Override
    public Page<TurntableLog> getTurntableLog(Long userId) {
        Map param = new HashMap<>();
        param.put("userId", userId);
        SqlParse sqlParse = new SqlParse(param);
        sqlParse.addSQL(" select * from t_turntable_log ");
        sqlParse.addSQL(" where user_id = :userId ");
        sqlParse.addSQL(" order by create_time desc ");
        return TurntableLog.dao.paginate(1, 30, sqlParse);
    }
}
